<template>
  <div class="container">
    <div class="export-header">
     <el-button icon="el-icon-download" type="primary" size='medium' @click="addItem">批量导入</el-button>
      <el-button  icon="el-icon-edit" type="primary" size='medium' @click="addBtn">添加</el-button>
     <el-button  icon="el-icon-upload2" type="primary" size='medium' @click="exportClick">导出</el-button>
     
    </div>
    <div class="input_modal">
      <div class="input-item">
        <span>用户名：</span>
        <el-input v-model="userName" placeholder="请输入内容" class="proName" @keyup.enter.native="search"></el-input>
      </div>
      <div class="input-item">
        <span>用户手机号：</span>
        <el-input v-model="mobile" placeholder="请输入内容" class="proName" @keyup.enter.native="search"></el-input>
        <el-button icon="el-icon-search" type="primary" size="medium" @click="getList">查询</el-button>
      </div>
    </div>
    <div class="table_con">
      <el-table v-loading="loading" :data="tableData"  :row-key="getRowKeys"  @selection-change="handleSelectionChange" :border='true'>
         <el-table-column
          fixed="left"
          :reserve-selection="true"
          type="selection"
          min-width='100'
          align="center"
        ></el-table-column> 
        <el-table-column :formatter="formatter" prop="avatarUrl " label="头像">
          <template slot-scope="scope">
            <div class="avatar" v-if='scope.row.weixinUserCoreDTO' alt="">
              <img :src="scope.row.weixinUserCoreDTO.headimgurl" alt="">
            </div>
            <div class="avatar" v-else><img src="../../images/default.png" alt=""></div>
          </template>
        </el-table-column>
        <el-table-column :formatter="formatter" prop="trueName" label="用户名">
          <template slot-scope="scope">
            <div>{{scope.row.weixinUserCoreDTO?scope.row.weixinUserCoreDTO.weixinUsername:''}}</div>
          </template>
          
        </el-table-column>
        <el-table-column :formatter="formatter" prop="mobile" label="用户手机号"></el-table-column>
        <el-table-column   prop="price" label="账户余额"></el-table-column>
        <el-table-column prop="" label="操作" width='120'>
          <template slot-scope="scope">
            <el-button class="link" type="primary" size="mini">
              <router-link :to="{ path: 'userDetail',query: {id: scope.row.userId, pageIndex:pageIndex}}" style="color: #fff;">查看</router-link>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pageIndex" :page-size="pageSize" :page-sizes='[10,20,30,40]' layout="total, sizes, prev, pager, next, jumper" :total="pageCount">
      </el-pagination>
    </div>
     <el-dialog
        title="批量导入用户"
        :visible.sync="dialogVisible1"
        width="40%"
       >
        <el-upload
          class="upload-demo"
          style="margin:0 auto;width:400px;"
          drag
          :file-list="fileList"
          :action="importUrl1"
          :on-success="handUploadSuccess"
          multiple
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        </el-upload>
      </el-dialog>
  </div>
</template>

<script>
let CONSTANT = require("../../constant/constant.js");
let common = require("../../common.js");
export default {
  data() {
    return {
      tableData: [],
      pageCount: null,
      pageIndex: 1,
      pageSize: 10,
      loading: true,
      trueName: null,
      mobile: null,
      userName:null,
      nickname:null,
      selectVal:[],
      ids:[],
      dialogVisible1:false,
      fileList: [],
      importUrl1: "",
    };
  },
  mounted() {
    this.pageIndex = this.$router.currentRoute.query.pageIndex
      ? this.$router.currentRoute.query.pageIndex
      : 1;
    this.sessionId = sessionStorage.getItem("sessionId");
    this.importUrl1 = `${CONSTANT.URL.LIST.userImport}?sessionId=${
      this.sessionId
    }`;
    this.getList();
  },
  methods: {
    handUploadSuccess(res) {
      console.log(res,'ee')
      if (res.status == -1) {
        this.$alert( res.msg||"导入失败!", '', {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
      } else {
        console.log(res)
        this.pageIndex = 1;
        this.getList();
        this.$message({
          type: "success",
          message: "导入成功!"
        });
      }
      this.dialogVisible1 = false
    },
    addItem(){
      this.dialogVisible1 = true
    },
    handleSelectionChange(val){
      this.ids = []
      this.selectVal = []
      console.log(val)
      this.selectVal = val
      this.selectVal.map(item=>{
        this.ids.push(item.userId)
      })
    },
    addBtn(){
      this.$router.push('userAdd?pageIndex='+this.pageIndex)
    },
    dayChange(e){
      console.log(e,'aaaaaaa')
    },
    getRowKeys(row) {
      return row.userId;
    },
    exportClick(){
      let sessionId = sessionStorage.getItem('sessionId');
      let url = `${CONSTANT.URL.LIST.userShopExport}?sessionId=${sessionId}`;
       var data = {
          ids:this.ids,
        type:'normal'
       }
       common.post1(url, JSON.stringify(data), res => {
         var blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
          blob = URL.createObjectURL(blob)
          var fileName = "用户.xlsx"
          let link = document.createElement('a')
          link.href = blob
          link.download = fileName
          link.click()
          link.remove()
      });
    },
    formatter(row, column, cellValue) {
      return cellValue ? cellValue : "- -";
    },
    getList: function() {
      let url = CONSTANT.URL.LIST.findEsShopUserCoreByPage;
      let data = {
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        userName: this.userName,
        mobile: this.mobile,
        type:'normal'
      };
      this.loading = true;
      common.requestAjax(url, JSON.stringify(data), null, res => {
        this.loading = false;
        let data = res.data;
        this.tableData = data.bussData;
        this.pageCount = data.count;
      });
    },
    search: function(e) {
      if (e.keyCode == 13) {
        this.pageIndex = 1;
        this.getList();
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getList();
    }
  }
};
</script>
